package org.jproxy.json;

import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

class ArrayJSONObject implements JSONObject
{
	private final List<JSONObject> array;
	private final Iterator<JSONObject> it;

	ArrayJSONObject(List<JSONObject> objList)
	{
		array = Collections.unmodifiableList(objList);
		it = array.iterator();
	}

	@Override
	public JSONAttribute getAttribute()
	{
		return null;
	}

	@Override
	public JSONAttribute getAttribute(String name)
	{
		return null;
	}

	public JSONObject getJSONObject()
	{
		return it.next();
	}

	@Override
	public Type getType()
	{
		return Type.ARRAY;
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T toObject(Class<T> clazz)
	{
		Class<?> componentClass = clazz.getComponentType();
		T[] tArray = (T[]) Array.newInstance(componentClass, array.size());
		for(int i = 0; i < array.size(); i++)
			tArray[i] = (T) array.get(i).toObject(componentClass);
		return (T) tArray;
	}

}
